{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55885ce8-54b4-4e35-b11d-98b4fda01b19",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 31.MPU6050陀螺仪加速度传感器实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：31.MPU6050陀螺仪加速度传感器实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：MPU6050陀螺仪加速度传感器实验\n",
    "MPU6050陀螺仪加速度传感器实验，mpu6050库进行操作，进行I2C控制方式，控制MPU6050陀螺仪加速度传感器，并在界面上打印出对应的加速度速度和陀螺仪数据！！！！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab179391-9c46-48fd-92a4-d95e587c591a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mpu6050 import mpu6050\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.初始化工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化I2C地址为0x68的MPU-6050传感器\n",
    "makerobo_sensor = mpu6050(0x68)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e02fbeb2-59db-4f0c-bf5c-78f4e5bd8804",
   "metadata": {},
   "source": [
    "## 3.循环函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e77514af-5ab2-4e49-a633-90fd484dcca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "    try:\n",
    "        while True:\n",
    "            # 从传感器中检索加速度计数据。\n",
    "            accel_data = makerobo_sensor.get_accel_data()\n",
    "            # 从传感器中检索陀螺仪数据。\n",
    "            gyro_data = makerobo_sensor.get_gyro_data()\n",
    "            # 从传感器检索温度数据。\n",
    "            temp = makerobo_sensor.get_temp()\n",
    "        \n",
    "            # 打印加速度计数据。\n",
    "            print(\"Accelerometer data\")\n",
    "            print(\"x: \" + str(accel_data['x']))\n",
    "            print(\"y: \" + str(accel_data['y']))\n",
    "            print(\"z: \" + str(accel_data['z']))\n",
    "        \n",
    "            # 打印陀螺仪数据。\n",
    "            print(\"Gyroscope data\")\n",
    "            print(\"x: \" + str(gyro_data['x']))\n",
    "            print(\"y: \" + str(gyro_data['y']))\n",
    "            print(\"z: \" + str(gyro_data['z']))\n",
    "        \n",
    "            # 以摄氏度打印温度\n",
    "            print(\"Temp: \" + str(temp) + \" C\")\n",
    "        \n",
    "            # 在下一个读取周期之前暂停0.5秒。\n",
    "            sleep(0.5)\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"Exit\")  # Exit on CTRL+C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 4.程序入口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accelerometer data\n",
      "x: -6.6319385986328125\n",
      "y: -0.9816226806640624\n",
      "z: -3.9935283691406247\n",
      "Gyroscope data\n",
      "x: 9.404580152671755\n",
      "y: -8.351145038167939\n",
      "z: 5.984732824427481\n",
      "Temp: 17.51823529411765 C\n",
      "Accelerometer data\n",
      "x: -7.9295958984374995\n",
      "y: -1.1228805786132812\n",
      "z: -3.385401147460937\n",
      "Gyroscope data\n",
      "x: -3.816793893129771\n",
      "y: 4.33587786259542\n",
      "z: 0.3816793893129771\n",
      "Temp: 17.51823529411765 C\n",
      "Accelerometer data\n",
      "x: -7.113173132324218\n",
      "y: -1.3072341064453123\n",
      "z: -4.548983154296875\n",
      "Gyroscope data\n",
      "x: -7.908396946564886\n",
      "y: 3.4580152671755724\n",
      "z: 0.9389312977099237\n",
      "Temp: 17.56529411764706 C\n",
      "Accelerometer data\n",
      "x: -7.242460021972656\n",
      "y: -1.3239935180664062\n",
      "z: -4.161122485351562\n",
      "Gyroscope data\n",
      "x: -7.473282442748092\n",
      "y: -1.8854961832061068\n",
      "z: 1.251908396946565\n",
      "Temp: 17.61235294117647 C\n",
      "Accelerometer data\n",
      "x: -7.3861121215820305\n",
      "y: -1.4963760375976562\n",
      "z: -4.106055847167968\n",
      "Gyroscope data\n",
      "x: -7.809160305343512\n",
      "y: 1.4961832061068703\n",
      "z: 1.4198473282442747\n",
      "Temp: 17.61235294117647 C\n",
      "Accelerometer data\n",
      "x: -7.252036828613281\n",
      "y: -1.3431471313476562\n",
      "z: -4.302380383300781\n",
      "Gyroscope data\n",
      "x: -7.198473282442748\n",
      "y: -3.2061068702290076\n",
      "z: 2.4580152671755724\n",
      "Temp: 17.61235294117647 C\n",
      "Accelerometer data\n",
      "x: -7.357381701660156\n",
      "y: -1.3239935180664062\n",
      "z: -4.290409374999999\n",
      "Gyroscope data\n",
      "x: -6.450381679389313\n",
      "y: 1.83206106870229\n",
      "z: -0.10687022900763359\n",
      "Temp: 17.659411764705883 C\n",
      "Accelerometer data\n",
      "x: -7.314286071777343\n",
      "y: -1.350329736328125\n",
      "z: -4.163516687011718\n",
      "Gyroscope data\n",
      "x: -7.351145038167939\n",
      "y: 0.20610687022900764\n",
      "z: -0.8091603053435115\n",
      "Temp: 17.659411764705883 C\n",
      "Accelerometer data\n",
      "x: -7.42441934814453\n",
      "y: -1.5203180541992187\n",
      "z: -4.017470385742187\n",
      "Gyroscope data\n",
      "x: -8.129770992366412\n",
      "y: 1.5038167938931297\n",
      "z: -0.07633587786259542\n",
      "Temp: 17.659411764705883 C\n",
      "Accelerometer data\n",
      "x: -7.333439685058593\n",
      "y: -1.2665326782226562\n",
      "z: -4.266467358398438\n",
      "Gyroscope data\n",
      "x: -9.267175572519085\n",
      "y: 1.8549618320610688\n",
      "z: 3.83206106870229\n",
      "Temp: 16.953529411764706 C\n",
      "Accelerometer data\n",
      "x: -7.390900524902343\n",
      "y: -1.235408056640625\n",
      "z: -4.3622354248046875\n",
      "Gyroscope data\n",
      "x: -8.236641221374045\n",
      "y: -0.6259541984732825\n",
      "z: -0.8702290076335878\n",
      "Temp: 18.412352941176472 C\n",
      "Accelerometer data\n",
      "x: -7.280767248535156\n",
      "y: -1.2952630981445312\n",
      "z: -4.211400720214844\n",
      "Gyroscope data\n",
      "x: -7.251908396946565\n",
      "y: 0.9923664122137404\n",
      "z: 0.916030534351145\n",
      "Temp: 17.659411764705883 C\n",
      "Accelerometer data\n",
      "x: -7.321468676757812\n",
      "y: -1.3096283081054687\n",
      "z: -3.938461730957031\n",
      "Gyroscope data\n",
      "x: -16.47328244274809\n",
      "y: 4.442748091603053\n",
      "z: 2.9618320610687023\n",
      "Temp: 16.953529411764706 C\n",
      "Accelerometer data\n",
      "x: -7.568071447753906\n",
      "y: -1.4724340209960938\n",
      "z: -4.094084838867187\n",
      "Gyroscope data\n",
      "x: -14.778625954198473\n",
      "y: 3.3053435114503817\n",
      "z: -7.0534351145038165\n",
      "Temp: 17.61235294117647 C\n",
      "Accelerometer data\n",
      "x: -7.247248425292969\n",
      "y: -1.4053963745117186\n",
      "z: -4.129997863769531\n",
      "Gyroscope data\n",
      "x: -5.213740458015267\n",
      "y: -0.1450381679389313\n",
      "z: 0.015267175572519083\n",
      "Temp: 18.412352941176472 C\n",
      "Accelerometer data\n",
      "x: -7.41244833984375\n",
      "y: -1.3024457031249999\n",
      "z: -4.299986181640625\n",
      "Gyroscope data\n",
      "x: -7.687022900763359\n",
      "y: 0.5648854961832062\n",
      "z: 1.0076335877862594\n",
      "Temp: 17.753529411764706 C\n",
      "Accelerometer data\n",
      "x: -7.963114721679687\n",
      "y: -3.076549133300781\n",
      "z: -5.331887097167969\n",
      "Gyroscope data\n",
      "x: -22.412213740458014\n",
      "y: 46.656488549618324\n",
      "z: 54.51145038167939\n",
      "Temp: 17.800588235294118 C\n",
      "Accelerometer data\n",
      "x: -1.0582371337890624\n",
      "y: -19.6133\n",
      "z: 5.72693037109375\n",
      "Gyroscope data\n",
      "x: -56.62595419847328\n",
      "y: -78.12213740458016\n",
      "z: -121.74809160305344\n",
      "Temp: 18.36529411764706 C\n",
      "Accelerometer data\n",
      "x: -5.145139367675781\n",
      "y: 0.1388636962890625\n",
      "z: 9.988609326171874\n",
      "Gyroscope data\n",
      "x: 77.18320610687023\n",
      "y: -15.106870229007633\n",
      "z: 47.14503816793893\n",
      "Temp: 17.047647058823532 C\n",
      "Accelerometer data\n",
      "x: -3.9911341674804683\n",
      "y: -1.7884686401367187\n",
      "z: 10.390835205078124\n",
      "Gyroscope data\n",
      "x: -6.343511450381679\n",
      "y: -2.9083969465648853\n",
      "z: -15.83969465648855\n",
      "Temp: 17.753529411764706 C\n",
      "Accelerometer data\n",
      "x: -3.380612744140625\n",
      "y: -1.9105729248046874\n",
      "z: 10.512939489746094\n",
      "Gyroscope data\n",
      "x: -7.66412213740458\n",
      "y: -0.7938931297709924\n",
      "z: 0.3435114503816794\n",
      "Temp: 17.800588235294118 C\n",
      "Accelerometer data\n",
      "x: -3.3638533325195312\n",
      "y: -1.8818425048828125\n",
      "z: 10.426748229980468\n",
      "Gyroscope data\n",
      "x: -7.702290076335878\n",
      "y: 2.9160305343511452\n",
      "z: 2.267175572519084\n",
      "Temp: 17.753529411764706 C\n",
      "Accelerometer data\n",
      "x: -3.4620156005859375\n",
      "y: -1.7645266235351562\n",
      "z: 10.520122094726561\n",
      "Gyroscope data\n",
      "x: -6.114503816793893\n",
      "y: 3.3206106870229006\n",
      "z: 4.67175572519084\n",
      "Temp: 17.84764705882353 C\n",
      "Accelerometer data\n",
      "x: -4.355052819824219\n",
      "y: -1.656787548828125\n",
      "z: 10.383652600097655\n",
      "Gyroscope data\n",
      "x: -9.099236641221374\n",
      "y: 27.580152671755727\n",
      "z: 20.83206106870229\n",
      "Temp: 17.800588235294118 C\n",
      "Accelerometer data\n",
      "x: -6.133944653320312\n",
      "y: 1.0989385620117187\n",
      "z: 11.037269653320312\n",
      "Gyroscope data\n",
      "x: -93.22137404580153\n",
      "y: 48.12977099236641\n",
      "z: 10.396946564885496\n",
      "Temp: 17.84764705882353 C\n",
      "Accelerometer data\n",
      "x: -12.215216870117187\n",
      "y: -2.50912333984375\n",
      "z: -2.01112939453125\n",
      "Gyroscope data\n",
      "x: -17.541984732824428\n",
      "y: -72.70992366412214\n",
      "z: 18.572519083969464\n",
      "Temp: 17.84764705882353 C\n",
      "Accelerometer data\n",
      "x: -6.0908490234374995\n",
      "y: -3.0789433349609374\n",
      "z: -1.3958195678710936\n",
      "Gyroscope data\n",
      "x: -10.938931297709924\n",
      "y: 12.938931297709924\n",
      "z: 2.0610687022900764\n",
      "Temp: 17.800588235294118 C\n",
      "Accelerometer data\n",
      "x: -9.311050256347656\n",
      "y: -2.1068974609374997\n",
      "z: -2.47081611328125\n",
      "Gyroscope data\n",
      "x: -5.938931297709924\n",
      "y: 10.15267175572519\n",
      "z: 3.015267175572519\n",
      "Temp: 17.89470588235294 C\n",
      "Accelerometer data\n",
      "x: -7.52737001953125\n",
      "y: -2.389413256835937\n",
      "z: -2.2026655273437497\n",
      "Gyroscope data\n",
      "x: -8.007633587786259\n",
      "y: -5.778625954198473\n",
      "z: 1.3969465648854962\n",
      "Temp: 17.941764705882353 C\n",
      "Accelerometer data\n",
      "x: -8.55448253173828\n",
      "y: -2.1835119140624997\n",
      "z: -2.3630770385742186\n",
      "Gyroscope data\n",
      "x: -5.213740458015267\n",
      "y: 6.229007633587786\n",
      "z: 0.6564885496183206\n",
      "Temp: 17.89470588235294 C\n",
      "Accelerometer data\n",
      "x: -8.032546569824218\n",
      "y: -2.123656872558594\n",
      "z: -2.5139117431640625\n",
      "Gyroscope data\n",
      "x: -10.282442748091603\n",
      "y: -0.5572519083969466\n",
      "z: 0.9618320610687023\n",
      "Temp: 17.89470588235294 C\n",
      "Accelerometer data\n",
      "x: -7.977479931640625\n",
      "y: -2.090138049316406\n",
      "z: -2.5426421630859375\n",
      "Gyroscope data\n",
      "x: -7.061068702290076\n",
      "y: 3.8396946564885495\n",
      "z: 1.8549618320610688\n",
      "Temp: 17.988823529411764 C\n",
      "Accelerometer data\n",
      "x: -8.003816149902343\n",
      "y: -2.032677209472656\n",
      "z: -2.540247961425781\n",
      "Gyroscope data\n",
      "x: -8.465648854961833\n",
      "y: 0.8091603053435115\n",
      "z: 1.1068702290076335\n",
      "Temp: 17.89470588235294 C\n",
      "Accelerometer data\n",
      "x: -8.078036401367187\n",
      "y: -2.0566192260742184\n",
      "z: -2.8323405639648436\n",
      "Gyroscope data\n",
      "x: -7.702290076335878\n",
      "y: 2.3053435114503817\n",
      "z: 1.2213740458015268\n",
      "Temp: 17.988823529411764 C\n",
      "Accelerometer data\n",
      "x: -8.061276989746093\n",
      "y: -1.9009961181640624\n",
      "z: -2.7269956909179687\n",
      "Gyroscope data\n",
      "x: -7.557251908396947\n",
      "y: 1.4885496183206106\n",
      "z: 1.2671755725190839\n",
      "Temp: 17.988823529411764 C\n",
      "Accelerometer data\n",
      "x: -8.068459594726562\n",
      "y: -1.9536685546875\n",
      "z: -2.8898014038085935\n",
      "Gyroscope data\n",
      "x: -8.0\n",
      "y: 1.6259541984732824\n",
      "z: 1.0\n",
      "Temp: 18.03588235294118 C\n",
      "Accelerometer data\n",
      "x: -8.037334973144532\n",
      "y: -1.9416975463867185\n",
      "z: -2.8754361938476563\n",
      "Gyroscope data\n",
      "x: -6.717557251908397\n",
      "y: 1.7938931297709924\n",
      "z: 0.9923664122137404\n",
      "Temp: 17.988823529411764 C\n",
      "Exit\n"
     ]
    }
   ],
   "source": [
    "# 程序入口\n",
    "if __name__ == \"__main__\":\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()       # 循环显示信息\n",
    "\texcept KeyboardInterrupt: # 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tprint(\"Exit\")  # Exit on CTRL+C"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
